package com.redxun.config;

import com.redxun.core.boot.config.CustomExceptionExceptionResolver;
import com.redxun.core.boot.config.DefaultWebMvcConfig;
import com.redxun.core.share.interceptor.SupportGradeInterceptor;
import com.redxun.interceptor.PermissionInterceptor;
import com.redxun.ureport.interceptor.UreportInterceptor;
import com.xxl.job.admin.controller.interceptor.CookieInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import java.util.List;

/**
 * WebMvcConfig.
 *
 * @author aj
 * @date 2019/8/5
 */
@Configuration
public class WebMvcConfig extends DefaultWebMvcConfig {

    @Autowired
    private CookieInterceptor cookieInterceptor;

    @Autowired
    private PermissionInterceptor permissionInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath*:/static/")
                .addResourceLocations("classpath:/public/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        super.addResourceHandlers(registry);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**");
        registry.addInterceptor(cookieInterceptor).addPathPatterns("/**");
        registry.addInterceptor(new SupportGradeInterceptor())
                .addPathPatterns(new String[]{"/**"});
        registry.addInterceptor(new UreportInterceptor())
                .addPathPatterns(new String[]{"/**"});
    }
    
    @Override
    protected void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
        exceptionResolvers.add(new CustomExceptionExceptionResolver());
    }
    
}
